package com.zhongyu.controller;

import lombok.SneakyThrows;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import org.apache.ibatis.io.Resources;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.InputStream;
import java.net.URLEncoder;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/sal/xxddck")
public class XDaoController {
    @Resource
    private DataSource dataSource;
/*查询*/
    /**
     * 导出Excel
     */
    @SneakyThrows
    @GetMapping("/exportExcel")
    public void exportEcel(HttpServletResponse response  ,String id,String providerIdName,String creatorName,String stockKeeperName) {
        System.out.println("输出来自页面携带的参数：" + id);
        System.out.println("输出来自页面携带的参数：" + providerIdName);
        InputStream inputStream = Resources.getResourceAsStream("jasper/xiaoShou.jasper");
        Connection conn = dataSource.getConnection();
        Map parameters = new HashMap<>();
        parameters.put("cid", id);
        parameters.put("prIdName",providerIdName);
        parameters.put("crName", creatorName);

        parameters.put("stName", stockKeeperName);
        JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, parameters, conn);
        String title = "xiao";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xlsx");
     /*   final OutputStream outputStream=response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint,outputStream);*/
        JRXlsxExporter exporter = new JRXlsxExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
        exporter.exportReport();
    }

    @SneakyThrows
    @GetMapping("/exportPdf")
    public void exportpdf(HttpServletResponse response, String id,String providerIdName,String creatorName,String stockKeeperName) {
        ServletOutputStream servletOutputStream = response.getOutputStream();
        System.out.println("输出来自页面携带的参数：" + id);
        System.out.println("输出来自页面携带的参数：" + providerIdName);
        InputStream inputStream = Resources.getResourceAsStream("jasper/xiaoShou.jasper");
        Connection conn = dataSource.getConnection();
        Map parameters = new HashMap<>();
        parameters.put("cid", id);
        parameters.put("prIdName",providerIdName);
        parameters.put("crName", creatorName);
        parameters.put("stName", stockKeeperName);
        JasperRunManager.runReportToPdfStream(inputStream, servletOutputStream, parameters, conn);
        response.setContentType("application/pdf");
        String title="xiao";
        response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xlsx");
        servletOutputStream.flush();
        servletOutputStream.close();
    }




    /*录入与出库*/
    /**
     * 导出Excel
     */
    @SneakyThrows
    @GetMapping("/exportE")
    public void exportE(HttpServletResponse response  ,String id,String providerIdName,String creatorName,String stockKeeperName) {
        System.out.println("输出来自页面携带的参数：" + id);
        System.out.println("输出来自页面携带的参数：" + providerIdName);
        InputStream inputStream = Resources.getResourceAsStream("jasper/xiaoShou.jasper");
        Connection conn = dataSource.getConnection();
        Map parameters = new HashMap<>();
        parameters.put("cid", id);
        parameters.put("prIdName",providerIdName);
        parameters.put("crName", creatorName);


        JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, parameters, conn);
        String title = "xiao";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xlsx");
     /*   final OutputStream outputStream=response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint,outputStream);*/
        JRXlsxExporter exporter = new JRXlsxExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
        exporter.exportReport();
    }

    @SneakyThrows
    @GetMapping("/exportP")
    public void exportP(HttpServletResponse response, String id,String providerIdName,String creatorName,String stockKeeperName) {
        ServletOutputStream servletOutputStream = response.getOutputStream();
        System.out.println("输出来自页面携带的参数：" + id);
        System.out.println("输出来自页面携带的参数：" + providerIdName);
        InputStream inputStream = Resources.getResourceAsStream("jasper/xiaoShou.jasper");
        Connection conn = dataSource.getConnection();
        Map parameters = new HashMap<>();
        parameters.put("cid", id);
        parameters.put("prIdName",providerIdName);
        parameters.put("crName", creatorName);

        JasperRunManager.runReportToPdfStream(inputStream, servletOutputStream, parameters, conn);
        response.setContentType("application/pdf");
        String title="xiao";
        response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xlsx");
        servletOutputStream.flush();
        servletOutputStream.close();
    }
}
